package other.link;

public class LinkAround {


    public static void main(String[] args) {
        //1.构建循环链表
        Node<Integer> first = null;
//记录前一个结点
        Node<Integer> pre = null;
        for (int i = 1; i <= 41; i++) {
//第一个元素
            if (i == 1) {
                first = new Node(i, null);
                pre = first;
                continue;
            }
            Node<Integer> node = new Node<>(i, null);
            pre.next = node;
            pre = node;
            if (i == 41) {
//构建循环链表，让最后一个结点指向第一个结点
                pre.next = first;
            }
        }

        int count=0;
        Node<Integer> n=first;
        Node<Integer> before=null;
        while (n.next!=n){
            count++;
            if(count==3){
                before.next=n.next;
                count=0;
                System.out.println("出圈:"+n.item);
            }else {
                before=n;
            }
            n=n.next;
        }
        System.out.println(n.item);




    }

    //结点类
    private static class Node<T> {
        //存储数据
        T item;
        //下一个结点
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
}
